//
//  ZJKSearchView.swift
//  MicroBlog
//
//  Created by VAD on 16/4/16.
//  Copyright © 2016年 朱江坤. All rights reserved.
//

import UIKit

class ZJKSearchView: UIView {

    @IBOutlet weak var cancelButton: UIButton!
    
    @IBOutlet weak var textField: ZJKTextField!

    @IBOutlet weak var textFieldRightCons: NSLayoutConstraint!
    
    class func searchView()->ZJKSearchView {
        return NSBundle.mainBundle().loadNibNamed("ZJKSearchView", owner: nil, options: nil).last! as! ZJKSearchView
    }
    
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
    
    override func awakeFromNib() {
        leftView.frame.size = CGSize(width: self.frame.height, height: self.frame.height)
        
        textField.leftView = leftView
        
        textField.leftViewMode = .Always
    }
    
    
    @IBAction func beginEditing(sender: ZJKTextField) {
        
        self.textFieldRightCons.constant = self.cancelButton.frame.width
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
    }
    
    @IBAction func cancelDidClick(sender: UIButton) {
        
        self.textFieldRightCons.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
        }
        
        self.textField.resignFirstResponder()
        
        
    }
    private lazy var leftView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        imageView.contentMode = .Center
        
        return imageView
}()
    
    
}